define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'advice/index' + location.search,
                    // add_url: 'advice/add',
                    // edit_url: 'advice/edit',
                    del_url: 'advice/del',
                    multi_url: 'advice/multi',
                    import_url: 'advice/import',
                    table: 'advice',
                }
            });

            var table = $("#table");
            // 初始化表格后添加点击事件
            table.on('post-body.bs.table', function () {
                $(".order-detail").on('click', function () {
                    var id = $(this).data('id');
                    // 这里复用操作列中的详情按钮逻辑
                    Fast.api.open('user/user/detail/ids/' + id, __('详情'), {
                        callback: function (data) {
                            Layer.alert("接收到回传数据：" + JSON.stringify(data), { title: "回传数据" });
                        },
                        area: ['80%', '80%']
                    });
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'user.nickname', title: __('用户'), operate: 'LIKE', formatter: function (value, row, index) {
                                let html = `<img src="${row.user.avatar}" style="width:30px;height:30px;border-radius:50%;margin-right:5px;">
                                <a href="javascript:;" class="order-detail" data-id="${row.user.id}">${row.user.nickname}</a>`;
                                return html;
                            }
                        },
                        // {field: 'user_id', title: __('User_id')},
                        {
                            field: 'content',
                            title: __('Content'),
                            operate: false,
                            formatter: function (value, row, index) {
                                return '<div style="text-align: center;">' + value + '</div>';
                            }
                        },
                        { field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'look',
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-dialog btn-info',
                                    icon: 'fa fa-eye',
                                    url: 'advice/look',
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        look: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
